from functools import wraps
from django.core.urlresolvers import reverse
from django.shortcuts import redirect
from core.views.auth.login import login


def login_required():
    def decorator(view):
        @wraps(view)
        def _view(request, *args, **kwargs):
            if request.session.get('user_id'):
                return view(request, *args, **kwargs)
            else:
                login_url = '{0}?return_to={1}'.format(reverse(login), request.get_full_path())
                return redirect(login_url)
        return _view
    return decorator
